<?php
/**
 * 按月写入个人考勤记录 到表report_history
 * 1.获取每个月应工作的天数
 * 2.根据用户获取用户记录
 */

apf_require_class('Oa_Job_Attendance_Abstract');
apf_require_class("Tools_Tools");
class Oa_Job_Attendance_ReportHistory extends Oa_Job_Attendance_Abstract {

    const MONTHWORKDAILY = 28;//当月工作结算日

    /** 考勤系统读 **/
    private $readdb = "attendance_slave";
    /** 考勤系统写 **/
    private $writedb = "attendance_master";
    //tools工具
    private $tools;

    public function run(){
        $this->tools = Tools_Tools::get_instance();

        //条件参数
        $work_att_day = $this->tools->current_month_work_day();//传参控制年月
        $params = array(
                'year'=>'2012',
                'month'=>'06'
                );

        //按月份获取所有用户信息
        $userlist = $this->search_users($params,$work_att_day);
        var_dump($userlist);
        $this->init_data($userlist,$params['year'].$params['month']);
    }

    /**
     * 数据写入操作
     */
    public function init_data($data,$yearmonth){
        $sql = "replace into report_history(user_id,yearmonth,act_days,late,compassionate,
                corptrip,trip,sick,yearleave,otherleave,absenteeism,last_deduction,otherleave_ids,lastleave_ids)
                values(?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
        foreach ($data as $d){
            $array = array(
                    $d['user_id'],//用户年假
                    $yearmonth,
                    $d['work_act_day'],
                    $d['oa_cd'],
                    $d['oa_sj'],
                    $d['oa_gc'],
                    $d['oa_cc'],
                    $d['oa_bj'],
                    $d['oa_nj'],
                    $d['oa_oj'],
                    0,
                    0,
                    '',
                    ''
            );
            $this->execute($sql,$array,$this->writedb);
        }
    }


    /**
     * 搜索用户
     * @param array $params
     * @param int $work_att_day 每月应工作多少天
     */
    public function search_users($params,$work_att_day) {

        //所有用户信息
        $result = $this->get_all_user_info();

        //组装用户的考勤信息
        if(!empty($result)){
            //当月第一第一天
            $time = $this->get_time($params['year'].$params['month'].'01');
            //用户的考勤记录
            $record_info = $this->get_users_record($time['start'],$time['end']);
            //获得用户的上月未扣数据<只获取用户的请假信息>
            //TODO$prex_record_info = $this->get_prex_record_info($time['start']);
            //用户请假信息
            $leave_info = $this->memger_leavetime($record_info['leave_info']);
            //用户迟到信息
            $belate_info = $this->memger_belateinfo($record_info['belate_info']);
            //用户公出信息
            $corptrip_info = $this->memger_corptripinfo($record_info['corptrip_info']);
            //用户出差信息
            $trip_info = $this->memger_tripinfo($record_info['trip_info']);
            //用户事假信息
            $sj_info = $this->memger_sjinfo($record_info['sj_info']);
            //用户病假信息
            $bj_info = $this->memger_bjinfo($record_info['bj_info']);
            //用户年假信息
            $nj_info = $this->memger_njinfo($record_info['nj_info']);
            //用户其他假信息
            $oj_info = $this->memger_ojinfo($record_info['oj_info']);

            $Ym = date('Ym');
            $_Ym = $params['select_year'].$params['select_month'];

            //组装
            foreach($result as $key=>$v) {
                //实际工作天数
                $work_act_day = 0;
                if($_Ym<=$Ym) {
                    $work_act_day = $work_att_day;
                }

                $oa_cd = 0;//迟到
                $oa_gc = 0;//公出
                $oa_cc = 0;//出差
                $oa_sj = 0;//事假
                $oa_bj = 0;//病假
                $oa_nj = 0;//年假
                $oa_oj = 0;//其他假
                $oa_prexj = 0;//上月未扣
                if(isset($leave_info[$v['user_id']])) {
                    $work_act_day = $work_att_day-$leave_info[$v['user_id']]['leave_time'];
                }
                if(isset($belate_info[$v['user_id']])) {
                    $oa_cd = $belate_info[$v['user_id']]['oa_cd'];
                }
                if(isset($corptrip_info[$v['user_id']])) {
                    $oa_gc = $corptrip_info[$v['user_id']]['leave_time'];
                }
                if(isset($trip_info[$v['user_id']])) {
                    $oa_cc = $trip_info[$v['user_id']]['leave_time'];
                }
                if(isset($sj_info[$v['user_id']])) {
                    $oa_sj = $sj_info[$v['user_id']]['leave_time'];
                }
                if(isset($bj_info[$v['user_id']])) {
                    $oa_bj = $bj_info[$v['user_id']]['leave_time'];
                }
                if(isset($nj_info[$v['user_id']])) {
                    $oa_nj = $nj_info[$v['user_id']]['leave_time'];
                }
                if(isset($oj_info[$v['user_id']])) {
                    $oa_oj = $oj_info[$v['user_id']]['leave_time'];
                }

                /* TODO
                if(isset($prex_record_info[$v['user_id']])) {
                    $oa_prexj = $prex_record_info[$v['user_id']]['leave_time'];
                }
                */
                $result[$key]['work_act_day'] = $work_act_day;
                $result[$key]['rice_day'] = floor($work_act_day);
                //餐费信息
                $result[$key]['meal'] = $this->get_meal($work_att_day,$work_act_day);
                $result[$key]['oa_cd'] = $oa_cd;
                $result[$key]['oa_gc'] = $oa_gc;
                $result[$key]['oa_cc'] = $oa_cc;
                $result[$key]['oa_sj'] = $oa_sj;
                $result[$key]['oa_bj'] = $oa_bj;
                $result[$key]['oa_nj'] = $oa_nj;
                $result[$key]['oa_oj'] = $oa_oj;
                $result[$key]['oa_prexj'] = $oa_prexj;
                // print_r($result);
            }
        }
        return $result;
    }

    /**
     * 所有时间段内请假记录
     * @param $start 开始时间 em:2012-06-01
     * @param $end 结束时间 em:2012-07-01
     * @return array(leave_info=>array(请假信息),'belate_info'=>array(迟到信息),'trip_info'=>array(公出信息))
     */
    public function get_users_record($start,$end){

        $result = array();

        $sql = "select * from leave_record where start_time >='{$start}' and end_time <= '{$end}'";

        $rs = $this->get_db_rows($sql, $this->readdb);

        if(empty($rs)) {
            return $result;
        }

        $record_type = array('丧假','事假','产假','产前假','产检','婚假','年假','病假','请假','陪产假','施加');//请假类型
        $belate_type = array('迟到');//迟到类型
        $corptrip_type = array('公出');//公出类型
        $trip_type = array('出差');//出差类型
        $sj_type = array('事假','施加');
        $bj_type = array('病假');
        $nj_type = array('年假');
        $other_type = array('丧假','产假','产前假','产检','婚假','请假','陪产假');
        foreach($rs as $v) {
            //请假条件 && 已批准
            if(in_array($v['type'],$record_type) && $v['status']=='已批准') {
                $tmp = array();
                $tmp['user_id'] = $v['user_id'];
                $tmp['leave_time'] = $v['leave_time'];
                $tmp['leave_msg'] = $start.'至'.$end;
                $result['leave_info'][] = $tmp;
            }
            //迟到 && 未批准
            if(in_array($v['type'],$belate_type) && $v['status']!='已批准') {
                $tmp = array();
                $tmp['user_id'] = $v['user_id'];
                $result['belate_info'][] = $tmp;
            }
            //公出 && 已批准
            if(in_array($v['type'],$corptrip_type) && $v['status']=='已批准') {
                $tmp = array();
                $tmp['user_id'] = $v['user_id'];
                $tmp['leave_time'] = $v['leave_time'];
                $result['corptrip_info'][] = $tmp;
            }
            //出差 && 已批准
            if(in_array($v['type'],$trip_type) && $v['status']=='已批准') {
                $tmp = array();
                $tmp['user_id'] = $v['user_id'];
                $tmp['leave_time'] = $v['leave_time'];
                $result['trip_info'][] = $tmp;
            }
            //事假 && 已批准
            if(in_array($v['type'],$sj_type) && $v['status']=='已批准') {
                $tmp = array();
                $tmp['user_id'] = $v['user_id'];
                $tmp['leave_time'] = $v['leave_time'];
                $result['sj_info'][] = $tmp;
            }
            //病假 && 已批准
            if(in_array($v['type'],$bj_type) && $v['status']=='已批准') {
                $tmp = array();
                $tmp['user_id'] = $v['user_id'];
                $tmp['leave_time'] = $v['leave_time'];
                $result['bj_info'][] = $tmp;
            }
            //年假 && 已批准
            if(in_array($v['type'],$nj_type) && $v['status']=='已批准') {
                $tmp = array();
                $tmp['user_id'] = $v['user_id'];
                $tmp['leave_time'] = $v['leave_time'];
                $result['nj_info'][] = $tmp;
            }
            //其它假 && 已批准
            if(in_array($v['type'],$other_type) && $v['status']=='已批准') {
                $tmp = array();
                $tmp['user_id'] = $v['user_id'];
                $tmp['leave_time'] = $v['leave_time'];
                $result['oj_info'][] = $tmp;
            }
        }
        return $result;
    }


    /**
     * 所有用户信息
     */
    public function get_all_user_info(){
        $sql = "select * from user_info where job_status<>'离职' and dimission_date='0000-00-00 00:00:00'";
        $result = $this->get_db_rows($sql, $this->readdb);
        return $result;
    }

    /**
     * 时间格式处理
     */
    public function get_time($time = 0) {
        $result = array('start'=>0,'end'=>'0');
        if(empty($time)) {
            return $result;
        }
        //echo $time;
        $time = strtotime($time);
        $month = date('m',$time);
        $start = date('Y-m-d',mktime(0,0,0,$month,1,date('Y',$time)));//本月开始
        $end = date('Y-m-d',mktime(0,0,0,$month+1,1,date('Y',$time)));//结束日期
        $end = $this->get_month_word_end_day(date('Ym',$time));
        $result['start'] = $start;
        $result['end'] = $end;
        return $result;
    }

    /**
     * 获得本月结束日
     */
    public function get_month_word_end_day($yearmonth = 0) {
        $Tools = Tools_Tools::get_instance();
        if(empty($yearmonth)) {
            $yearmonth = date('Ym');
        }
        $workendday = $yearmonth.self::MONTHWORKDAILY;
        //查找数据库里是否有当月的结算时间
        $bll_monthset = $this->init_month_set();
        $month_set = $bll_monthset->get_month_special_set($yearmonth);
        if(!empty($month_set['settlement'])) {
            $workendday = substr($month_set['settlement'],0,10);
        }
        return $workendday;
    }

    /**
     * 初始化monthspecialset bll
     * @return Bll_Attendance_MonthSpecialSet
     */
    public function init_month_set() {
        apf_require_class("Bll_Attendance_MonthSpecialSet");
        return new Bll_Attendance_MonthSpecialSet();
    }

    /**
     * 组合用户其他假信息
     */
    public function memger_ojinfo($rs) {
        $result = $this->memger_leavetime($rs);
        return $result;
    }

    /**
     * 组合用户年假信息
     */
    public function memger_njinfo($rs) {
        $result = $this->memger_leavetime($rs);
        return $result;
    }

    /**
     * 组合用户病假信息
     */
    public function memger_bjinfo($rs) {
        $result = $this->memger_leavetime($rs);
        return $result;
    }

    /**
     * 组合用户事假信息
     * @param unknown_type $rs
     */
    public function memger_sjinfo($rs) {
        $result = $this->memger_leavetime($rs);
        return $result;
    }

    /**
     * 组合用户出差信息
     * @param unknown_type $rs
     * @return multitype:|Ambigous <multitype:, unknown>
     */
    public function memger_tripinfo($rs) {
        $result = $this->memger_leavetime($rs);
        return $result;
    }

    /**
     * 组合用户公出信息
     * @param unknown_type $rs
     * @return array(userid=>公出天数)
     */
    public function memger_corptripinfo($rs) {
        $result = $this->memger_leavetime($rs);
        //         if(!empty($result)) {
        //             foreach($result as $uid => $info) {
        //                 $result[$uid]['leave_time'] =  sprintf("%.1f",$result[$uid]['leave_time']/8);
        //             }
        //         }
        return $result;
    }

    /**
     * 组合用户迟到信息
     * @param unknown_type $rs
     * @return array(userid=>迟到次数)
     */
    public function memger_belateinfo($rs) {
        $result = array();
        if(empty($rs)) {return array();}
        foreach($rs as $key=>$v) {
            if(isset($result[$v['user_id']])) {
                $result[$v['user_id']]['oa_cd'] += 1;
            }else {
                $result[$v['user_id']]['oa_cd'] = 1;
            }
        }
        return $result;
    }

    /**
     * 组合用户请假信息
     * @param unknown_type $rs
     * @return array(userid=>请假天数)
     */
    public function memger_leavetime($rs) {
        $result = array();
        if(empty($rs)) {return array();}
        foreach($rs as $v) {
            if(isset($result[$v['user_id']])) {
                $result[$v['user_id']]['leave_time'] += $v['leave_time'];
            } else {
                $result[$v['user_id']]['leave_time'] = $v['leave_time'];
            }
        }
        return $result;
    }

    /**
     * 餐补计算
     * @param unknown_type $att_day 应出勤天数
     * @param unknown_type $act_day 实出勤天数
     */
    public function get_meal($att_day,$act_day) {
        $meal = APF::get_instance()->get_config('oa_meal');
        $meal = round($meal/$att_day*$act_day);
        return $meal;
    }

}